package com.wpre.springhomework;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description: 实现用户登录
 * User: LianBao
 * Date: 2024-10-25
 * Time: 13:37
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public boolean login(String userName, String password, HttpSession session) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        //设置Session
        session.setAttribute("userName", userName);


        return "admin".equals(userName) && "admin".equals(password);
    }

    @RequestMapping(value = "/getLogin", method = RequestMethod.GET)
    public String getLogin(HttpSession session) {

        //获取Session
        String userName = (String) session.getAttribute("userName");

        return userName;
    }
}
